#include "dialog.h"
#include <QDebug>
#include "ui_dialog.h"
#include <QGroupBox>
//#include

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    Lat = "";
    flip = true;
    Long = "";
    Range = "";
    setLayout(ui->MainLayout);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_PacketFilterButton_clicked()
{
    ui->RangeFilterParam->setDisabled(true);
    FilterString = "t/";
    qDebug() << "Packet Type Filter =  " << FilterString;
    ui->LatitudeFilter->clear();
    ui->LongitudeFilter->clear();
    ui->RangeFilter->clear();
    ui->PacketFilterType->setDisabled(false);
}

void Dialog::on_RangeFilterButton_clicked()
{
    ui->RangeFilterParam->setDisabled(false);
    FilterString = "r///";
    qDebug() << "Packet Type Filter =  " << FilterString;
    ui->PacketFilterType->setDisabled(true);
    ui->PositionPacketFilter->setChecked(false);
    ui->TelemetryPacketFilter->setChecked(false);
    ui->NWSPacketFilter->setChecked(false);
    ui->WeatherPacketFilter->setChecked(false);
}

void Dialog::on_ResetFilterButton_clicked()
{
    ui->RangeFilterParam->setDisabled(true);
    FilterString = "";
    ui->PacketFilterType->setDisabled(true);
    ui->LatitudeFilter->clear();
    ui->LongitudeFilter->clear();
    ui->RangeFilter->clear();
    ui->PositionPacketFilter->setChecked(false);
    ui->TelemetryPacketFilter->setChecked(false);
    ui->NWSPacketFilter->setChecked(false);
    ui->WeatherPacketFilter->setChecked(false);
}

void Dialog::on_PortNumber_textChanged(QString )
{

}

void Dialog::on_LatitudeFilter_textChanged(QString text)
{
 Lat = text;
 FilterString = "r/" + Lat + "/" + Long + "/" + Range;
 qDebug() << "Packet Type Filter =  " << FilterString;
}

void Dialog::on_LongitudeFilter_textChanged(QString text)
{
    Long = text;
    FilterString = "r/" + Lat + "/" + Long + "/" + Range;
    qDebug() << "Packet Type Filter =  " << FilterString;

}

void Dialog::on_RangeFilter_textChanged(QString text)
{
    Range = text;
    FilterString = "r/" + Lat + "/" + Long + "/" + Range;
    qDebug() << "Packet Type Filter =  " << FilterString;

}

void Dialog::on_PositionPacketFilter_clicked()
{
 if(ui->PositionPacketFilter->isChecked())
 {
  if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/p";
  }
  if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pt";
  }
  if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pnt";
  }
  if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pntw";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pnw";
  }
  if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/ptw";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pn";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
    FilterString = "t/pw";
  }
 }
 else
 {
  if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/";
  }
  if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/t";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/n";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/w";
  }
  if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && !ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/nt";
  }
  if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/tw";
  }
  if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/nw";
  }
  if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
      && ui->WeatherPacketFilter->isChecked())
  {
   FilterString = "t/ntw";
  }
 }
 qDebug() << "Packet Type Filter =  " << FilterString;
}

void Dialog::on_TelemetryPacketFilter_clicked()
{
    if(ui->TelemetryPacketFilter->isChecked())
    {
     if(!ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/t";
     }
     if(ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pt";
     }
     if(ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pnt";
     }
     if(ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pntw";
     }
     if(!ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/ntw";
     }
     if(ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/ptw";
     }
     if(!ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/nt";
     }
     if(!ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/tw";
     }
    }
    else
    {
     if(!ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/";
     }
     if(ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/p";
     }
     if(!ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/n";
     }
     if(!ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/w";
     }
     if(ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/pn";
     }
     if(ui->PositionPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/pw";
     }
     if(!ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/nw";
     }
     if(ui->PositionPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/pnw";
     }
    }
    qDebug() << "Packet Type Filter =  " << FilterString;
}

void Dialog::on_NWSPacketFilter_clicked()
{
    if(ui->NWSPacketFilter->isChecked())
    {
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/n";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/nt";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pnt";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pntw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pnw";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/ntw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/pn";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
       FilterString = "t/nw";
     }
    }
    else
    {
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/t";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/p";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/w";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && !ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/pt";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/tw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/pw";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->PositionPacketFilter->isChecked()
         && ui->WeatherPacketFilter->isChecked())
     {
      FilterString = "t/ptw";
     }
    }
    qDebug() << "Packet Type Filter =  " << FilterString;
}

void Dialog::on_WeatherPacketFilter_clicked()
{
    if(ui->WeatherPacketFilter->isChecked())
    {
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/w";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/tw";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/ntw";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/pntw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/pnw";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/ptw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/nw";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
       FilterString = "t/pw";
     }
    }
    else
    {
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/t";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/n";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/p";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && !ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/nt";
     }
     if(ui->TelemetryPacketFilter->isChecked() && !ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/pt";
     }
     if(!ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/pn";
     }
     if(ui->TelemetryPacketFilter->isChecked() && ui->NWSPacketFilter->isChecked()
         && ui->PositionPacketFilter->isChecked())
     {
      FilterString = "t/pnt";
     }
    }
    qDebug() << "Packet Type Filter =  " << FilterString;
}

void Dialog::on_Connect_clicked()
{
    if (flip == true)
    {
     QString ConnectString = "user TEST1 pass -1 vers testsoftware v1 " + FilterString;
     qDebug() << "Authentication String =  " << ConnectString << endl;
     qDebug() << "Authentication Bytes Written:  " << endl;
     qDebug() << "Last Socket Error =  " << endl;
     qDebug() << "Socket State =  " << endl;

     ui->Connect->setText("Disconnect");
     flip = false;
      ui->RangeFilterParam->setDisabled(true);
      ui->ServerFilter->setDisabled(true);
      ui->PacketFilterType->setDisabled(true);
    }
    else
    {
     ui->Connect->setText("Connect");
     flip = true;
     ui->RangeFilterParam->setDisabled(false);
     ui->ServerFilter->setDisabled(false);
     ui->PacketFilterType->setDisabled(false);
    }
}
